﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Perceiveit.Xml.Converters
{
    public abstract class FormattableXConverter<T> : XConverter<T>
    {
        private IFormatProvider _formatter;
        private string _format;

        public IFormatProvider Formatter
        {
            get { return this._formatter; }
        }

        public string Format
        {
            get { return this._format; }
        }


        public FormattableXConverter(string format, IFormatProvider formatProvider)
        {
            _formatter = formatProvider;
            _format = format;
        }

        public override string ToString(T value)
        {
            IFormattable f = (IFormattable)value;
            if (null == value)
                return string.Empty;
            else
                return f.ToString(_format, _formatter);
        }
    }
}
